package com.arm.controller.shoppingCart;

import com.arm.controller.WechatBaseController;
import com.arm.def.Constant;
import com.arm.module.Order;
import com.arm.module.ShoppingCart;
import com.arm.module.User;
import com.arm.service.ShoppingCartService;
import com.arm.service.UserService;
import com.arm.tools.myRunTime.MyRunTimeExcption;
import com.arm.tools.result.ResultObj;
import com.arm.tools.result.ReturnCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

//import com.github.pagehelper.PageHelper;
//import com.github.pagehelper.PageInfo;

/**
 * Created by baggio on 2016/12/27.
 */
@RequestMapping("/shoppingCart")
@RestController
public class ShoppingCartController extends WechatBaseController {

    @Autowired
    ShoppingCartService shoppingCartService;

    @Autowired
    private UserService userService;

    @RequestMapping(value = "/shoppingCartList")
    public ModelAndView orderList(HttpServletRequest request, HttpServletResponse response) {        // source: 1(来源于网站) | 2(来源于微站)
        ModelAndView mov = new ModelAndView();
        mov.setViewName("wechat/order/shoppingCartList");
        if (super.getIsMobile(request)) {
            super.oauth2(request, response);
        }
        User user = (User) session.getAttribute(Constant.SESSION_KEY_USER_LOGIN);
        if (user != null) {

            List<ShoppingCart> shoppingCartList = shoppingCartService.getShoppingCartListByUserId(user.getTid());

            mov.addObject("shoppingCartList", shoppingCartList);
        }


        return mov;
    }

    @PostMapping(value = "/addShoppingCart")
    public String addShoppingCart(ShoppingCart shoppingCart) {
        ResultObj resultObj = new ResultObj();
        try {
            User user = (User) super.session.getAttribute(Constant.SESSION_KEY_USER_LOGIN);
            if (user == null) {
                throw new MyRunTimeExcption("用户不存在，添加失败！");
            }
            shoppingCart.setUserId(user.getTid());
            shoppingCartService.addShoppingCart(shoppingCart);
        } catch (MyRunTimeExcption runTimeExcption) {
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(runTimeExcption.getMessage());
            runTimeExcption.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultObj.toJson();
    }

    @PostMapping(value = "/deleteShoppingCart")
    public String deleteShoppingCart(String shoppingCartIds) {
        ResultObj resultObj = new ResultObj();
        try {
            String shoppingCartIdArray[] = shoppingCartIds.split(",");

            for (String shoppingCartId : shoppingCartIdArray
                    ) {
                shoppingCartService.deleteShoppingCart(Long.parseLong(shoppingCartId));
            }

        } catch (MyRunTimeExcption runTimeExcption) {
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(runTimeExcption.getMessage());
            runTimeExcption.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultObj.toJson();
    }
    @PostMapping(value = "/payShoppingCart")
    public String payShoppingCart(String shoppingCartIds) {
        ResultObj resultObj = new ResultObj();
        try {
            Order order=shoppingCartService.payShoppingCart(shoppingCartIds,(User) super.session.getAttribute(Constant.SESSION_KEY_USER_LOGIN));
            resultObj.setData(order);
        } catch (MyRunTimeExcption runTimeExcption) {
            resultObj.setCode(ReturnCode.FAIL.getCode());
            resultObj.setMsg(runTimeExcption.getMessage());
            runTimeExcption.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return resultObj.toJson();
    }

}
